﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;

namespace TeamDeployer.TeamFoundation.Build.Workflow.CustomTypes
{
    internal class DeploySpecListConverter : ExpandableObjectConverter
    {
        // Methods
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return ((destinationType == typeof(string)) ||
                    base.CanConvertTo(context, destinationType));
        }

        public override object ConvertTo(ITypeDescriptorContext context,
                                         CultureInfo culture,
                                         object value,
                                         Type destinationType)
        {
            DeploySpecList source = value as DeploySpecList;
            if ((source != null) && (destinationType == typeof(string)))
            {
                int num = source.Count<DeploySpec>(p => p.Enabled);
                if (num > 1)
                {
                    return string.Format("{0} sets of packages enabled.", num);
                }
                if (num == 1)
                {
                    return source.First<DeploySpec>().Name;
                }
                return "No packages enabled.";
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}